package org.jtao.gbsubject.service;

import com.lkx.util.ExcelUtil;
import org.jtao.gbsubject.domain.College;
import org.jtao.gbsubject.domain.Subject;
import org.jtao.gbsubject.domain.SubjectClazz;
import org.jtao.gbsubject.model.SubjectExcelModel;
import org.jtao.gbsubject.repository.CollegeRepository;
import org.jtao.gbsubject.repository.SubjectClazzRepository;
import org.jtao.gbsubject.repository.SubjectRepository;
import org.jtao.gbsubject.service.dto.SubjectDTO;
import org.jtao.gbsubject.service.mapper.SubjectClazzMapper;
import org.jtao.gbsubject.service.mapper.SubjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Set;


/**
 * Service Implementation for managing Subject.
 */
@Service
@Transactional
public class SubjectService {

    private final Logger log = LoggerFactory.getLogger(SubjectService.class);

    private final SubjectRepository subjectRepository;

    private final SubjectMapper subjectMapper;

    private final SubjectClazzRepository subjectClazzRepository;

    private final SubjectClazzMapper subjectClazzMapper;

    private final CollegeRepository collegeRepository;

    public SubjectService(SubjectRepository subjectRepository, SubjectMapper subjectMapper, SubjectClazzRepository subjectClazzRepository, SubjectClazzMapper subjectClazzMapper, CollegeRepository collegeRepository) {
        this.subjectRepository = subjectRepository;
        this.subjectMapper = subjectMapper;
        this.subjectClazzRepository = subjectClazzRepository;
        this.subjectClazzMapper = subjectClazzMapper;
        this.collegeRepository = collegeRepository;
    }

    /**
     * Save a subject.
     *
     * @param subjectDTO the entity to save
     * @return the persisted entity
     */
    public SubjectDTO save(SubjectDTO subjectDTO) {
        log.debug("Request to save Subject : {}", subjectDTO);
        Subject subject = subjectMapper.toEntity(subjectDTO);
        subject = subjectRepository.save(subject);
        return subjectMapper.toDto(subject);
    }

    /**
     * Get all the subjects.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<SubjectDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Subjects");
        return subjectRepository.findAll(pageable)
            .map(subjectMapper::toDto);
    }


    /**
     * Get one subject by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public SubjectDTO findOne(Long id) {
        log.debug("Request to get Subject : {}", id);
        Subject subject = subjectRepository.findOneWithEagerRelationships(id);
        return subjectMapper.toDto(subject);
    }

    public Page<SubjectDTO> findAllByCollegeID(Long id) {
        log.debug("Request to get subjects by collegeId");
        return subjectRepository.findAllByCollegesId(id, null).map(subjectMapper::toDto);
    }

    /**
     * Delete the subject by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Subject : {}", id);
        subjectRepository.delete(id);
    }

    public Integer importSubject(MultipartFile file) throws Exception {
        List<SubjectExcelModel> subjectList = ImportService.importExcel(file, 0, 1, SubjectExcelModel.class);
        int sum = 0;
        int update = 0;
        for (int i = 0; i < subjectList.size(); i++) {
            SubjectExcelModel subjectExcel = subjectList.get(i);
            Subject subjectNew = new Subject();
            if (subjectExcel.getName() != null) {
                String subjectName = subjectExcel.getName();
                subjectNew.setName(subjectName);

                String subjectCode = subjectExcel.getCode();
                subjectNew.setCode(subjectCode);

                Integer subjectSum = subjectExcel.getStudentSum();
                subjectNew.setStudentSum(subjectSum);

                try {
                    String subjectClazzName = subjectExcel.getSubjectClazzName();
                    SubjectClazz subjectClazzDB = subjectClazzRepository.findOneByName(subjectClazzName);
                    if (subjectClazzDB != null) {
                        subjectNew.setSubjectClazz(subjectClazzDB);
                    }

                    String collegesName = subjectExcel.getCollegesName();
                    College collegeDB = collegeRepository.findOneByName(collegesName);
                    subjectNew.addCollege(collegeDB);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Subject subjectDB = subjectRepository.findOneByName(subjectExcel.getName());
                //判断数据库中是否存在该专业，如果不存在则进行数据持久化。
                if (subjectDB == null) {
                    sum++;
                    this.save(subjectMapper.toDto(subjectNew));
                } else {
                    //数据库中存在，则进行更新
                    subjectNew.setId(subjectDB.getId());
                    update++;
                    this.save(subjectMapper.toDto(subjectNew));
                }
            }
        }
        System.out.println("导入数据一共【" + sum + "】行," + "更新数据一共[" + update + "]行。");
        return sum;
    }

}
